#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/Connector/lib -I/data/Software/mydan/Connector/private/lib
use strict;
use warnings;
use utf8;
use YAML::XS;
use JSON;
use Logs;
use LWP::UserAgent;
use uuid;

$| ++;

=head1 SYNOPSIS

    approver => '',
    submitter => '',
    content => '',

=cut

my ( $approver, %env );
BEGIN{
    use Configini;
    $approver = Configini::get( 'approver' );
    %env = Configini::env( 'approverenv' );
    use HTTP::Message;
    *HTTP::Message::_utf8_downgrade = sub{};
};

return sub
{
    my %param = @_;

    if( $approver )
    {
        my $ua = LWP::UserAgent->new;
        $ua->default_header( %env ) if %env;

        my $cont = JSON::to_json( \%param );
        utf8::encode($cont);

        my $res = $ua->post(
            $approver,
            Content => $cont, 
            'Content-Type' => 'application/json' 
        );

        if($res->is_success)
        {
            my $v = eval{decode_json $res->decoded_content};
            return $v->{data} if $v->{stat};
        }
        #TODO
        die "die" . YAML::XS::Dump $approver;
    }
    else
    {
        return uuid->new()->create_str;
    }
}
